﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Zhuang.DAL.Access;

namespace Zhuang.WinForm.BasicDataSetting
{
    public partial class ProductList : Form
    {
        ProductDAL productDAL = new ProductDAL();

        public ProductList()
        {
            InitializeComponent();
        }

        

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();

        }

        private void LoadData()
        {
            
            dgvMain.DataSource = productDAL.Get(tbCondition.Text.Trim());
        }


        private void tsbAdd_Click(object sender, EventArgs e)
        {
            ProductAdd pa = new ProductAdd();
            pa.ShowDialog();
            if (pa.DialogResult == DialogResult.Yes)
            {
                LoadData();
            }
            
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
           

            if (dgvMain.SelectedRows.Count > 0)
            {
                string productName = dgvMain.SelectedRows[0].Cells["ProductName"].Value.ToString();
                DialogResult dr = MessageBox.Show("确认要删除商品[" + productName + "]吗？", "提示", MessageBoxButtons.OKCancel);

                if (dr == DialogResult.OK)
                {
                    int id = Convert.ToInt32(dgvMain.SelectedRows[0].Cells["ProductId"].Value);
                    productDAL.Delete(id);

                    LoadData();

                }
                
            }
            else
            {
                MessageBox.Show("请选择要删除的商品！");
            }

            
        }

        private void tsbModify_Click(object sender, EventArgs e)
        {
            if (dgvMain.SelectedRows.Count > 0)
            {
                int id= Convert.ToInt32(dgvMain.SelectedRows[0].Cells["ProductId"].Value);

                ProductAdd pa = new ProductAdd(id);
                pa.ShowDialog();
                if (pa.DialogResult == DialogResult.Yes)
                {
                    LoadData();
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的商品！");
            }
        }

        private void ProductList_Load(object sender, EventArgs e)
        {
            dgvMain.AutoGenerateColumns = false;
        }
    }
}
